package util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class SmsSendUtil {
	
	public final static String APP_KEY = "23309278";
    public final static String APP_SECRET = "a34b39a426932f0a7743903bcabb7935";
    public final static String APP_URL = "http://gw.api.taobao.com/router/rest";
    
	public static String sendSmsPwd(String tel, String code, String pwd){
		if("+86".equals(code)){
			TaobaoClient client = new DefaultTaobaoClient(SmsConfigUtil.APP_URL, SmsConfigUtil.APP_KEY, SmsConfigUtil.APP_SECRET);
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend( "" );
			req.setSmsType( "normal" );
			req.setSmsFreeSignName( "友米科技" );
			req.setSmsParamString( "{code:'"+pwd+"'}" );
			req.setRecNum(tel);
			req.setSmsTemplateCode( "SMS_80140045" );
			AlibabaAliqinFcSmsNumSendResponse rsp;
			try {
				rsp = client.execute(req);
				return pwd;
			} catch (ApiException e) {
				e.printStackTrace();
				return null;
			}
		}else{
			return sendOtherCountrySmsPwd(tel, code, pwd);
		}
	}
	
	private static String sendOtherCountrySmsPwd(String tel, String code, String p) {
		if(code.startsWith("+")){
			code = code.replace("+", "");
		}
		String ip = "210.51.190.233";
        int port = 8085;
        // HTTP 请求工具
        HttpClientUtil util = new HttpClientUtil(ip,  port, SmsConfigUtil.COUNTRY_URL);
        String user = SmsConfigUtil.COUNTRY_ACCOUNT;//你的用户名 
        String pwd = SmsConfigUtil.COUNTRY_PWD;//你的密码：
        String ServiceID = "SEND"; //固定，不需要改变
        String dest = code + tel; // 你的目的号码【收短信的电话号码】
        String sender = "";// 你的原号码,可空【大部分国家原号码带不过去，只有少数国家支持透传，所有一般为空】
        String msg = "【友米科技】恭喜您成功注册友米乐，您的初始密码为"+p+"，请登录客户端，体验全球新社交购物生活的精彩与不同。";//你的短信内容

        // codec=8 Unicode 编码,  3 ISO-8859-1, 0 ASCII
        // 短信内容 HEX 编码，8 为 UTF-16BE HEX 编码， dataCoding = 8 ,支持所有国家的语言，建议直接使用 8
        String hex = WebNetEncode.encodeHexStr(8, msg); 
        hex = hex.trim() + "&codec=8";
//        System.out.println("POST MT3");
        // HTTP 封包请求, util.sendPostMessage  返回结果，
        // 如果是以 “-” 开头的为发送失败，请查看错误代码，否则为MSGID
        String re = util.sendPostMessage( user,  pwd,  ServiceID,  dest,  sender,  hex);
//        System.out.println(dest);
//        System.out.println(re);
        if(re != null && !re.startsWith("-")){
        	return p;
        }else{
        	return null;
        }
//        System.out.println("msgid = " +);
	}
	
	
	public static String sendSms(String tel, String code){
		if("+86".equals(code)){
			String sms_no = StringUtil.getRandomNumberString(6);
			TaobaoClient client = new DefaultTaobaoClient(SmsConfigUtil.APP_URL, SmsConfigUtil.APP_KEY, SmsConfigUtil.APP_SECRET);
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend( "" );
			req.setSmsType( "normal" );
			req.setSmsFreeSignName( "友米科技" );
			req.setSmsParamString( "{code:'"+sms_no+"',minute:'15'}" );
			req.setRecNum(tel);
			req.setSmsTemplateCode( "SMS_86505025" );
			AlibabaAliqinFcSmsNumSendResponse rsp;
			try {
				rsp = client.execute(req);
				return sms_no;
			} catch (ApiException e) {
				e.printStackTrace();
				return null;
			}
		}else{
			return sendOtherCountrySms(tel, code);
		}
	}

	private static String sendOtherCountrySms(String tel, String code) {
		if(code.startsWith("+")){
			code = code.replace("+", "");
		}
		String sms_no = StringUtil.getRandomNumberString(6);
		String ip = "210.51.190.233";
        int port = 8085;
        // HTTP 请求工具
        HttpClientUtil util = new HttpClientUtil(ip,  port, SmsConfigUtil.COUNTRY_URL);
        String user = SmsConfigUtil.COUNTRY_ACCOUNT;//你的用户名 
        String pwd = SmsConfigUtil.COUNTRY_PWD;//你的密码：
        String ServiceID = "SEND"; //固定，不需要改变
        String dest = code + tel; // 你的目的号码【收短信的电话号码】
        String sender = "";// 你的原号码,可空【大部分国家原号码带不过去，只有少数国家支持透传，所有一般为空】
        String msg = "【Uniplaza】Your Uniplaza verification code is "+sms_no;//你的短信内容

        // codec=8 Unicode 编码,  3 ISO-8859-1, 0 ASCII
        // 短信内容 HEX 编码，8 为 UTF-16BE HEX 编码， dataCoding = 8 ,支持所有国家的语言，建议直接使用 8
        String hex = WebNetEncode.encodeHexStr(8, msg); 
        hex = hex.trim() + "&codec=8";
//        System.out.println("POST MT3");
        // HTTP 封包请求, util.sendPostMessage  返回结果，
        // 如果是以 “-” 开头的为发送失败，请查看错误代码，否则为MSGID
        String re = util.sendPostMessage( user,  pwd,  ServiceID,  dest,  sender,  hex);
//        System.out.println(dest);
//        System.out.println(re);
        if(re != null && !re.startsWith("-")){
        	return sms_no;
        }else{
        	return null;
        }
//        System.out.println("msgid = " +);
	}
	
	 
	 public static void main(String[] args){
//		 String url = DwzUtil.generateShortUrl("www.uniplaza.com");
//		 System.out.println(url);
		 //String tel, String code, String good_name, String price, String url
		 String a = "15944342886";
//		 sendSmsLimitedNotify(a,null,"测试商品","100.00","http://wap.uniposts.com/index/goodDetail.do?good_id=1000");
	}
}
